package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo7 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT s.name,s.gender,s.age,s.job,c.name,c.floor " +
                    "FROM student s " +
                    "JOIN class c ON c.id = s.class_id " +
                    "WHERE c.name = '1年级1班'";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            System.out.printf("%-8s %-4s %-4s %-8s %-8s %-4s%n",
                    "学生姓名", "性别", "年龄", "职位", "班级名", "楼层");
            while (rs.next()) {
                String name = rs.getString("s.name");
                String gender = rs.getString("s.gender");
                int age = rs.getInt("s.age");
                String job = rs.getString("s.job");
                String className = rs.getString("c.name");
                String floor = rs.getString("c.floor");
                System.out.println(name + " \t " + gender + "\t " + age + "\t " + job + "\t " + className + "\t " + floor);
            }

        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}
